package com.bolt.core.view.tag;

import com.bolt.core.view.components.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.Writer;
import java.util.List;


/**
 *　JSP表单元素标签
 * @author  michao
 */
public class FormItemTemplateTag extends BodyTagSupport {

	private static final Log LOG = LogFactory.getLog(FormItemTemplateTag.class);

	private Form form = null;

	public FormTemplateTag getParentTag() {
		return ((FormTemplateTag) this.getParent());
	}

	@Override
	public int doEndTag() throws JspException {
        HttpServletRequest request =(HttpServletRequest) pageContext.getRequest();
        HttpServletResponse response =(HttpServletResponse) pageContext.getResponse();

		form = getParentTag().getForm();

		List<List<FormItem>> group = form.getGroup();
        group.forEach(k->{
            k.forEach(i->{
                i.setTemplateDir(form.getTemplateDir());
                i.setTheme(form.getTheme());
                i.setTemplateEngine(form.getTemplateEngine());
            });
        });
        Writer writer = pageContext.getOut();
		for (List<FormItem> row : group) {
            printFormItem(request,response,writer, row);
		}
	

		for (FormItem item : form.getHiddenFormItems()) {
            item.start(request,response,pageContext.getOut());
            item.end(request,response,pageContext.getOut());
        }
		return EVAL_PAGE;
	}
	
	public void addValidator(FormItem item){
		if("combination".equals(item.getType())){
			CombinationFormItem combination = (CombinationFormItem)item;
			for(FormItem formItem :combination.getCombinations() ){
				addValidator(formItem);
			}
		}else{
			ValidatorRule v = form.findValidator(item.getId());
			 if(v != null){
				 item.addParameter("validator", v);
			 }
		}		 
	}

	private void printFormItem(HttpServletRequest request, HttpServletResponse response, Writer writer,List<FormItem> items) {
		try {
			if (items.size() == 1 && items.get(0) instanceof FieldSet) {
				FieldSet fieldset = ((FieldSet) items.get(0));
				fieldset.start(request,response,writer);
				List<List<FormItem>> group = fieldset.getGroup();
				for (List<FormItem> row : group) {
                    printFormItem(request,response,writer,row);
				}
				fieldset.end(request,response,writer);
			} else {
				Decorate row = Decorate.buildRowDecorate();
				row.start(request,response,writer);
				for (FormItem item : items) {
					if (item.isUpdateHidde() && getParentTag().isUpdateState()) {
						continue;
					}
                    addValidator(item);
					Decorate column = Decorate.buildFormColumnDecorate(item);
                    column.start(request,response,writer);
                    column.end(request,response,writer);
				}
				row.end(request,response,writer);
			}

		} catch (Exception ex) {
			LOG.error(ex);
		}
	}

	@Override
    public int doStartTag() throws JspException {

		return SKIP_BODY;
	}


}
